package base.caseDemo;

import cn.hutool.core.util.StrUtil;

import java.util.List;

public class RealDemo {
  private RecordInfo prevA;
  private long totalB;
  
  private RecordInfo prevB;
  private long totalA;
  
  
  public void apply(List<RecordInfo> list) {
    // 类似：一条一条 读文件
    for (RecordInfo recordInfo : list) {
      if (recordInfo.getType().equals("a"))
        singleApplyA(recordInfo);
      else
        singleApplyB(recordInfo);
    }
    
    if (prevA != null) {
      prevA.setMoney(totalA);
      System.out.println(prevA);
    }
    
    if (prevB != null) {
      prevB.setMoney(totalB);
      System.out.println(prevB);
    }
  }
  
  public void singleApplyA(RecordInfo recordInfo) {
    // prev == ""
    if (this.prevA == null) {
      prevA = recordInfo;
      // total = total + money;
      totalA = recordInfo.getMoney();
    } else {
      if (StrUtil.equals(prevA.getKey(), recordInfo.getKey())) {
        totalA += recordInfo.getMoney();
      } else {
        prevA.setMoney(totalA);
        System.out.println(prevA);
        
        prevA = recordInfo;
        // 清空
        // 赋值
        totalA = recordInfo.getMoney();
      }
    }
  }
  
  public void singleApplyB(RecordInfo recordInfo) {
    // prev == ""
    if (this.prevB == null) {
      prevB = recordInfo;
      // total = total + money;
      totalB = recordInfo.getMoney();
    } else {
      if (StrUtil.equals(prevB.getKey(), recordInfo.getKey())) {
        totalB += recordInfo.getMoney();
      } else {
        prevB.setMoney(totalB);
        System.out.println(prevB);
        
        prevB = recordInfo;
        // 清空
        // 赋值
        totalB = recordInfo.getMoney();
      }
    }
  }
}
